home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / lib2to3 / fixes / fix_map.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  2.7 KB  |  53 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. """Fixer that changes map(F, ...) into list(map(F, ...)) unless there
  5. exists a 'from future_builtins import map' statement in the top-level
  6. namespace.
  7.  
  8. As a special case, map(None, X) is changed into list(X).  (This is
  9. necessary because the semantics are changed in this case -- the new
  10. map(None, X) is equivalent to [(x,) for x in X].)
  11.  
  12. We avoid the transformation (except for the special case mentioned
  13. above) if the map() call is directly contained in iter(<>), list(<>),
  14. tuple(<>), sorted(<>), ...join(<>), or for V in <>:.
  15.  
  16. NOTE: This is still not correct if the original code was depending on
  17. map(F, X, Y, ...) to go on until the longest argument is exhausted,
  18. substituting None for missing values -- like zip(), it now stops as
  19. soon as the shortest argument is exhausted.
  20. """
  21. from pgen2 import token
  22. from  import fixer_base
  23. from fixer_util import Name, Call, ListComp, in_special_context
  24. from pygram import python_symbols as syms
  25.  
  26. class FixMap(fixer_base.ConditionalFix):
  27.     PATTERN = "\n    map_none=power<\n        'map'\n        trailer< '(' arglist< 'None' ',' arg=any [','] > ')' >\n    >\n    |\n    map_lambda=power<\n        'map'\n        trailer<\n            '('\n            arglist<\n                lambdef< 'lambda'\n                         (fp=NAME | vfpdef< '(' fp=NAME ')'> ) ':' xp=any\n                >\n                ','\n                it=any\n            >\n            ')'\n        >\n    >\n    |\n    power<\n        'map'\n        args=trailer< '(' [any] ')' >\n    >\n    "
  28.     skip_on = 'future_builtins.map'
  29.     
  30.     def transform(self, node, results):
  31.         if self.should_skip(node):
  32.             return None
  33.         if node.parent.type == syms.simple_stmt:
  34.             self.warning(node, 'You should use a for loop here')
  35.             new = node.clone()
  36.             new.set_prefix('')
  37.             new = Call(Name('list'), [
  38.                 new])
  39.         elif 'map_lambda' in results:
  40.             new = ListComp(results.get('xp').clone(), results.get('fp').clone(), results.get('it').clone())
  41.         elif 'map_none' in results:
  42.             new = results['arg'].clone()
  43.         elif in_special_context(node):
  44.             return None
  45.         new = node.clone()
  46.         new.set_prefix('')
  47.         new = Call(Name('list'), [
  48.             new])
  49.         new.set_prefix(node.get_prefix())
  50.         return new
  51.  
  52.  
  53.